package com.example.teaculture.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.example.teaculture.R;
import com.example.teaculture.activity.FavorDetailActivity;
import com.example.teaculture.adapter.HeadlineBaseAdapter;
import com.example.teaculture.application.MyApplication;
import com.example.teaculture.model.Favor;
import com.example.teaculture.model.Headline;
import com.example.teaculture.view.RefreshableView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;

public class HeadLinesFragment extends androidx.fragment.app.Fragment {

    RefreshableView refreshableView;
    Handler handler;
    List<Headline> teaHeadlines;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.headlines_fragment, container, false);
        ListView lv_view = view.findViewById(R.id.list_view);
        teaHeadlines = MyApplication.getInstance().teaHeadlines;
        HeadlineBaseAdapter baseAdapter = new HeadlineBaseAdapter(getContext(), teaHeadlines);
        lv_view.setAdapter(baseAdapter);

        handler = new Handler(message -> {
            if (message.what == 0) {

                HeadlineBaseAdapter baseAdapter1 = new HeadlineBaseAdapter(getContext(), teaHeadlines);
                lv_view.setAdapter(baseAdapter1);

            }
            return false;
        });
        lv_view.setOnItemClickListener((adapterView, view1, i, l) -> {
            Favor favor = new Favor();
            favor.setTitle(teaHeadlines.get(i).getTitle());
            favor.setUrl(teaHeadlines.get(i).getUrl());
            MyApplication.getInstance().favor = favor;
            Intent intent = new Intent(getContext(), FavorDetailActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.putExtra("url", teaHeadlines.get(i).getUrl());
            intent.putExtra("title", teaHeadlines.get(i).getTitle());
            startActivity(intent);
        });


        refreshableView = (RefreshableView) view.findViewById(R.id.refreshable_view);
        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                try {
                    teaHeadlines.clear();
                    long l = System.currentTimeMillis();
                    Document doc = Jsoup.connect("http://cha.quanxi.cc/toutiao/TypesList/7030/1").get();
                    Elements elements = doc.select("div.left");
                    elements = elements.select("a.news-item-txtImg");
                    for (Element element : elements) {
                        String image = element.select("div.txtImg-img").select("el-image").attr("src");
                        String title = element.select("div.txtImg-img").select("el-image").attr("alt");
                        Element element1 = element.select("div.txtImg-txt").select("div.foot").get(0);
                        String info = element.select("div.txtImg-txt").select("div.des").text();
                        String date = element1.child(2).text();
                        String url = element.attr("href");
                        url = "http://cha.quanxi.cc/" + url;
                        teaHeadlines.add(new Headline(title, url, info, date, image));
                    }
                    Message message = Message.obtain();
                    message.what = 0;
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                refreshableView.finishRefreshing();
            }
        }, 0);

        return view;
    }
}

